package com.jk.controller.account;

import com.jk.dto.JsonResult;
import com.jk.entity.account.AccMenu;
import com.jk.service.account.AccMenuService;
import com.jk.util.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Map;

/**
 * Created by yanghuan on 15-8-31.
 */
@RequestMapping("/account/menu")
@Controller
public class MenuController {
    @Resource
    private AccMenuService accMenuService;

    //菜单管理
    @RequestMapping("/page")
    public String manageMenu() {
        return "account/menu";
    }

    //创建
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public String createMenu(AccMenu accMenu) {
        try {
            accMenuService.insert(accMenu);
            return JsonUtil.toString(new JsonResult(200, "保存成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new JsonResult(201, "保存失败"));
        }
    }

    //删除
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public String delMenu(String id) {
        try {
            accMenuService.delete(id);
            return JsonUtil.toString(new JsonResult(200, "删除成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new JsonResult(201, "删除失败"));
        }
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public String list() {
        try {
            return JsonUtil.toString(accMenuService.selectAllShowMenus());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new ArrayList<AccMenu>());
        }
    }

    @RequestMapping(value = "first")
    @ResponseBody
    public String first() {
        try {
            return JsonUtil.toString(accMenuService.selectShowMenus("root"));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new ArrayList<AccMenu>());
        }
    }


    @RequestMapping(value = "second")
    @ResponseBody
    public String second(String id) {
        try {
            return JsonUtil.toString(accMenuService.selectShowMenus(id));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new ArrayList<AccMenu>());
        }
    }

    @RequestMapping(value = "permissions")
    @ResponseBody
    public String permissions(String accMenuId) {
        try {
            if (null == accMenuId) {
                return JsonUtil.toString(new JsonResult(201, "请选择需要授权的菜单"));
            }
            return JsonUtil.toString(accMenuService.getPermissions(accMenuId));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new ArrayList<Map<String, Object>>());
        }
    }

    //授权
    @RequestMapping(value = "/grant", method = RequestMethod.POST)
    @ResponseBody
    public String grantPermissions(@RequestParam(value = "accMenuId", required = false) String accMenuId,
                                   @RequestParam(value = "perIdArray[]", required = false) String[] perIdArray) {
        try {
            if (null == accMenuId) {
                return JsonUtil.toString(new JsonResult(201, "请选择所授权的菜单"));
            }
            if (null == perIdArray) {
                perIdArray = new String[0];
            }
            accMenuService.grantPermissions(accMenuId, perIdArray);
            return JsonUtil.toString(new JsonResult(200, "授权成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new JsonResult(201, "授权失败"));
        }
    }
}
